package cn.com.kblog.intercepor;





import cn.com.kblog.util.LoginUtils;

import com.jfinal.aop.Interceptor;
import com.jfinal.core.ActionInvocation;
import com.jfinal.log.Logger;


public class LoginCheckInterceptor implements Interceptor {
	 public static Logger log = Logger.getLogger(LoginCheckInterceptor.class);
	@Override
	public void intercept(ActionInvocation ai) {
		LoginUtils.initConext(ai.getController().getRequest(), ai
				.getController().getResponse());
		try {
			_go(ai);
			LoginUtils.clearContext();
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		
	}

	private void _go(ActionInvocation ai) {
		String uri = ai.getController().getRequest().getRequestURI();
		log.debug("-------check URI:"+ uri);
		ai.getControllerKey();
		//不是后台管理不予拦截
		if(!uri.contains("/admin")){
			ai.invoke();
			return;
		}
		//不需要权限操作的Aciton
		if(ai.getControllerKey().equals("/admin")){
			ai.invoke();
			return;
		}
		//已登录
		if(LoginUtils.isLogin()){
			ai.invoke();
			return;
		}
		//未登录
		ai.getController().redirect(ai.getController().getRequest().getContextPath()+"/admin");
	}

	

}
